﻿namespace EyeOpen.BomberMan
{
	using System.Collections.Generic;
	using System.Drawing;
	using System.Reflection;

	public class GameDesigner
	{
		private readonly GameEnvironment gameEnvironment;
		private readonly IList<Image> imageList;

		public GameDesigner(ImageInitializer imageInitializer, GameEnvironment gameEnvironment, Size cellSize)
		{
			this.gameEnvironment = gameEnvironment;
			imageList = imageInitializer.Get(cellSize);
			CellSize = cellSize;
			SetFieldSize();
		}

		public int Height { get; private set; }

		public int Width { get; private set; }

		public Size CellSize { get; private set; }
		
		public void Paint(Graphics graphics)
		{
			graphics.Clear(Color.Transparent);

			for (var r = 0; r < gameEnvironment.Map.Size.Height; r++)
			{
				for (var c = 0; c < gameEnvironment.Map.Size.Width; c++)
				{
					var x = c * CellSize.Width;
					var y = r * CellSize.Height;

					var type = gameEnvironment.Map[c, r];
					var image = imageList[(int)type];
					graphics.DrawImage(image, x, y);
				}
			}
		}

		private void SetFieldSize()
		{
			Width = CellSize.Width * gameEnvironment.Map.Size.Width;
			Height = CellSize.Height * gameEnvironment.Map.Size.Height;
		}
	}
}